package com.openvas.core.gas.until;

import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@Component
public class SendMessage {
	
	private RestTemplate restTemplate;
	
	private HttpHeaders headers;
	


	public SendMessage(RestTemplate restTemplate, HttpHeaders headers) {
		super();
		this.restTemplate = restTemplate;
		this.headers = headers;
	}



	public String faSong(String time,String pozition,String mi,List<String>phoneList) {
		JSONObject obj=new JSONObject();
		obj.put("temp_id", 161878);
		obj.put("tag", "震动预警");		
		JSONArray array=new JSONArray();//recipients
		for (int i = 0; i < phoneList.size(); i++) {
			JSONObject temp=new JSONObject();
	        temp.put("mobile", phoneList.get(i));
	        JSONObject temp1=new JSONObject();
	        temp.put("temp_para", temp1);	     
	        temp1.put("date",time  );
	        temp1.put("pozition", pozition);
	        //temp1.put("mi", mi); 	        
	        array.add(temp);			
		}
		obj.put("recipients", array);
		return sendShakeMessage(obj);
	}
	public String tongji(String time,List<String> event,String min,List<String>phoneList) {
		String eventStr="";
		for (String op:event) {
			eventStr+=op;
		}
		event.clear();
		JSONObject obj=new JSONObject();
		obj.put("temp_id", 154364);
		obj.put("tag", "震动统计");		
		JSONArray array=new JSONArray();//recipients
		for (int i = 0; i < phoneList.size(); i++) {
			JSONObject temp=new JSONObject();
	        temp.put("mobile", phoneList.get(i));
	        JSONObject temp1=new JSONObject();
	        temp.put("temp_para", temp1);	     
	        temp1.put("date",time);
	        temp1.put("event",eventStr);
	        temp1.put("min", min); 	        
	        array.add(temp);			
		}
		obj.put("recipients", array);
		return sendShakeMessage(obj);
	}
	
	public String otdr(String time,String event,List<String>phoneList) {
		JSONObject obj=new JSONObject();
		obj.put("temp_id", 155866);
		obj.put("tag", "otdr预警");		
		JSONArray array=new JSONArray();//recipients
		for (int i = 0; i < phoneList.size(); i++) {
			JSONObject temp=new JSONObject();
	        temp.put("mobile", phoneList.get(i));
	        JSONObject temp1=new JSONObject();
	        temp.put("temp_para", temp1);	     
	        temp1.put("date",time);
	        temp1.put("event",event);        
	        array.add(temp);			
		}
		obj.put("recipients", array);
		return sendShakeMessage(obj);
	}
	
	
	
	public String sbgj(String time,List<String> event,String min,List<String>phoneList) {
		String eventStr="";
		for (String op:event) {
			eventStr+=op;
		}
		event.clear();
		JSONObject obj=new JSONObject();
		obj.put("temp_id", 154364);
		obj.put("tag", "设备告警");		
		JSONArray array=new JSONArray();//recipients
		for (int i = 0; i < phoneList.size(); i++) {
			JSONObject temp=new JSONObject();
	        temp.put("mobile", phoneList.get(i));
	        JSONObject temp1=new JSONObject();
	        temp.put("temp_para", temp1);	     
	        temp1.put("date",time);
	        temp1.put("event",eventStr);
	        temp1.put("min", min); 	        
	        array.add(temp);			
		}
		obj.put("recipients", array);
		return sendShakeMessage(obj);
	}
	
	
	
	public String sendShakeMessage(JSONObject obj) {	
		String response = this.restTemplate
		.exchange("https://api.sms.jpush.cn/v1/messages/batch",
				HttpMethod.POST, new HttpEntity<JSONObject>(obj, this.headers), String.class)
		.getBody();
	   return response;
	}
	


}
